import requests
import json
import time
import random

s = requests.Session()

#此处填入手机密码
phone = ""
password = ""

#此处parent_id填入存放上传文件目录的ID, 具体怎么得到请看说明
#默认值为0, 表示私人云的根目录, 建议修改后使用
parent_id = "0"

#此处设置是否开启上传文件签到功能, 1为开启, 默认开启
IS_UPLOAD = 1

#-------------------------------------------------

def main():
    (cookie, private_id) = login()
    if (IS_UPLOAD != 0):
        upload(cookie, private_id)
        (cookie, private_id) = login()
    checkIn(cookie)


def login():
    headers = {     
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36 Edg/83.0.478.58",
        # "User-Agent":"Mozilla/5.0 (Linux; Android 7.1.2; M6 Note Build/N2G47H; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/65.0.3325.110 Mobile Safari/537.36",
        "Content-Type": "application/x-www-form-urlencoded",
        "Accept-Encoding": "gzip"
    }

    login_URl = "https://quqi.com/auth/person/login/password"

    datas = {
        "password": password,
        "phone": phone
    }

    response = s.post(login_URl,headers = headers, data = datas)
    cookie = response.cookies

    group_list_URl = "https://group.quqi.com/v1/group/list"
    group_list = s.get(group_list_URl, cookies = cookie, headers = headers)
    #取一个不是私人云的群组id,默认取第一个
    group_id = json.loads(group_list.text)["data"][1]["quqi_id"]

    #取私人云盘id
    private_id = json.loads(group_list.text)["data"][0]["quqi_id"]
    # print(private_id)

    enter_group_URL = "https://quqi.com/api/dir/ls?limit=30&node_id=0&offset=0&order_type=0&quqi_id={}&tree_id=1"
    #访问一次群组
    temp = s.get(enter_group_URL.format(group_id), headers = headers, cookies = cookie)
    return cookie, private_id



def checkIn(cookie):
    #POST
    checkin_URL_today = "https://exp.quqi.com/v1/checkIn"
    #GEt
    checkin_URL_group = "https://exp.quqi.com/v1/dailyTask/sendAward/14"
    checkIn_URL_upload = "https://exp.quqi.com/v1/dailyTask/sendAward/15"

    checkin_headers = {
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Origin": "https://quqi.com",      
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36 Edg/83.0.478.58",
        # "User-Agent": "Mozilla/5.0 (Linux; Android 7.1.2; M6 Note Build/N2G47H; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/65.0.3325.110 Mobile Safari/537.36",
        "Referer": "https://quqi.com/p/mobile/gainBeans.html?barHeight=69",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,en-US;q=0.8",
        "X-Requested-With": "com.quqi.quqioffice"
    }
    checkin_response = s.post(checkin_URL_today, headers = checkin_headers, cookies = cookie)
    print("每日签到结果:")
    if(json.loads(checkin_response.text)["msg"] == "success"):
        print("每日签到成功\n")
    else:
        print(json.loads(checkin_response.text)["msg"])

    #先休眠5秒 等待群组访问生效
    time.sleep(5)
    checkin_group_response = s.get(checkin_URL_group, headers = checkin_headers, cookies = cookie)
    print("\n逛群组奖励领取结果:")
    if(json.loads(checkin_group_response.text)["msg"] == "success"):
        print("逛群组签到成功\n")
    else:
        print(json.loads(checkin_group_response.text)["msg"])

    if (IS_UPLOAD !=0):
        checkIn_upload_res = s.get(checkIn_URL_upload, headers = checkin_headers, cookies = cookie)
        print("文件上传奖励:")
        if(json.loads(checkIn_upload_res.text)["msg"] == "success"):
            print("文件上传奖励领取成功\n")
        else:
            print(json.loads(checkIn_upload_res.text)["msg"])


def upload(cookie, private_id):
    
    header = {
        # "User-Agent":"Mozilla/5.0 (Linux; Android 7.1.2; M6 Note Build/N2G47H; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/65.0.3325.110 Mobile Safari/537.36",
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36 Edg/83.0.478.58",
        "Content-Type": "application/x-www-form-urlencoded",
        "Host": "quqi.com",
        "Accept": "*/*",
        "Origin": "https://quqi.com",
        "Sec-Fetch-Site": "same-origin",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Dest": "empty",
        "Referer": "https://quqi.com/{}/{}".format(parent_id, private_id),
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "zh,en-US;q=0.9,en;q=0.8,zh-CN;q=0.7,zh-TW;q=0.6"
    }
    checkUploadURl = "https://quqi.com/api/doc/checkUploadFile?quqiid={}".format(private_id)

    client_id = getClientId()
    # print(client_id)
    data = {
        "quqi_id":"{}".format(private_id),
        "tree_id":"1",
        "parent_id":"{}".format(parent_id),
        "size":"14",
        "filename":"1.txt",
        "md5":"4928c30f7e95af99de24579485604457",
        "client_id":client_id
    }

    res = s.post(checkUploadURl, data=data, headers = header, cookies = cookie)
    if ("check_node_name" in res.text):
        data["upload_select"] = 1
        res = s.post(checkUploadURl, data=data, headers = header, cookies = cookie)

    print("upload status: " + json.loads(res.text)["msg"])



def getClientId():
    t = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    i = []
    n = len(t)
    for r in range(0,10):
        index = 0 | random.randint(0,61)
        i.append(t[index])
    id = "".join(i)
    client_id = "quqipc_" + id
    return client_id


def main_handler(event, context):
    main()

if __name__ == '__main__':
    main()
# dict = requests.utils.dict_from_cookiejar(cookie)
# print(dict)

